const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const buildPath = path.join(__dirname, './build');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProduction = nodeEnv === 'production';
module.exports = {
    mode: nodeEnv,
    entry: path.resolve(__dirname, 'src/index.jsx'),
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'index.js'
    },
    devtool: isProduction ? false : 'inline-source-map',
    plugins: [
        new htmlWebpackPlugin({
            filename: 'index.html',
            template: './index.html',
        }),
        new ExtractTextPlugin('style.css')
    ],
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                include: [
                    path.resolve(__dirname, 'src')
                ],
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.less$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'less-loader'],
                }),
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader',
                }),
            },
            {
                test: /\.(png|gif|jpg)$/,
                // include: imgPath,
                use: 'url-loader?limit=2020480&name=images/[name]-[hash].[ext]'
            },
            {
                test: /\.(ico|ttf|eot|svg)(\?v=[0-9].[0-9].[0-9])?$/,
                loader: 'file-loader?name=[name].[ext]'
            },
            {
                test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
                loader: 'url-loader?limit=10000&mimetype=application/font-woff'
            },
            {
                type: 'javascript/auto',
                test: /\.json$/,
                loader: 'json-loader',
            }
        ]
    },
    devServer: {
        host: '0.0.0.0',
        contentBase: buildPath,
        historyApiFallback: {index: '/'},
        hot: true,
        inline: true,
        port: 4000,
        open: true,
        disableHostCheck: true,
        proxy: [{
            context: ['/api'],
            target: 'http://localhost:9001'
        }]
    }
};